import sys
import re
import scrapy
from scrapy import Request, Selector

sys.path.append('../spiders')
from Taobao.items import TaobaoItem


class TaobaoSpider(scrapy.Spider):
    name = "taobaospider"
    allowed_domains = ["taobao.com"]

    def start_requests(self):
        keywords = ['家装']
        for k in keywords:
            for page in range(50):
                url = f'https://s.taobao.com/search?q={k}&s={44 * page}'
                yield Request(url=url)

    def parse(self, response, **kwargs):
        sel = Selector(response)

        selectors = sel.css('div.items > div.item.J_MouserOnverReq > div.ctx-box.J_MouseEneterLeave.J_IconMoreNew')
        for selector in selectors:
            item = TaobaoItem()
            item['title'] = ''.join(selector.css('div.row.row-2.title > a::text').extract()).strip()
            item['price'] = selector.css(
                'div.row.row-1.g-clearfix > div.price.g_price.g_price-highlight > strong::text').extract_first().strip()
            item['sole'] = selector.css('div.row.row-1.g-clearfix > div.deal-cnt::text').extract_first().strip()
            item['shop'] = selector.css(
                'div.row.row-3.g-clearfix > div.shop > a > span:nth-child(2)::text').extract_first().strip()
            item['address'] = selector.css('div.row.row-3.g-clearfix > div.location::text').extract_first().strip()

            yield item
